# mysite/views.py
from django.shortcuts import render
from django.http import JsonResponse, HttpResponse
## @csrf_exempt         ## 豁免特定视图
from django.views.decorators.csrf import csrf_exempt
##@csrf_protect         ## 保护特定视图
from django.views.decorators.csrf import csrf_protect
from django.views.decorators.http import require_http_methods
from yonghu.models import YongHu

## 首页
def index(request):
    context = {
        'welcome_message': '欢迎来到我的网站！',
    }
    return render(request, 'index.html', context)

## 注册
def register(request):
    context = {
    }
    return render(request, 'register.html', context)

## 注册
@require_http_methods(["GET","POST"])
@csrf_protect
def registerDo(request):
    pdata = request.POST
    username = request.POST.get('username')
    context = {
        'username': username,
        'pdata': pdata,
    }
    yonghu = YongHu(
        username=request.POST.get('username'), 
        email=request.POST.get('email'), 
        password=request.POST.get('password'), 
        salt="12345")
    yonghu.save() 
    return JsonResponse(context)
    #return HttpResponse('OK!!!!')

## 登录
def login(request):
    context = {
    }
    return render(request, 'login.html', context)

## 登出
def logout(request):
    context = {
    }
    return render(request, 'logout.html', context)